/**
* Copyright(c) 2019 六六微科技 Co. Ltd.
* All right reserved.
/*
* 管理员模块
* @author 小六子
* @date 2019年9月22日
*/
import axios from '../../axios'
import Vue from 'vue'
import { USER_TYPE } from "@/store/mutation-types"

// 保存
export const saveOrUpdate = (data) => {
  return axios({
    url: '/permission/admin/saveOrUpdate',
    method: 'post',
    data
  })
}
// 修改密码
export const modifyPassword = (data) => {
  return axios({
    url: '/permission/admin/modifyPassword',
    method: 'post',
    data
  })
}
// 获取
export const get = (id) => {
  return axios({
    url: '/permission/admin/get',
    method: 'get',
    params: {
      id: id
    }
  })
}
// 删除
export const remove = (id) => {
  return axios({
    url: '/permission/admin/remove',
    method: 'get',
    params: {
      id: id
    }
  })
}
// 批量删除
export const batchRemove = (data) => {
  return axios({
    url: '/permission/admin/remove',
    method: 'post',
    data
  })
}
// 查询
export const query = (data) => {
  return axios({
    url: '/permission/admin/query',
    method: 'post',
    data
  })
}

// 登录
export const login = data => {
  return axios({
    url: '/permission/login/login',
    method: 'post',
    data
  })
}

// 登出
export const logout = () => {
  return axios({
    url: '/permission/login/logout',
    method: 'get'
  })
}

// 获取菜单
export const getMenus = (parentUri) => {
  const data = {};
  return axios({
    url: '/permission/login/getMenus',
    method: 'post',
    data
  }).then((res)=>{
    var menus = []
    if(res.ok){
      var menus = toMenu(res.data.children, 0);
      res.data = menus;
    }
    return res;
  })
}


function toMenu(children, level){
  var menus = []
  for(let data of children){
    var menu = {}
    menu["url"] =  data.uri
    if("admin" == Vue.ls.get(USER_TYPE)){      
      if(level == 1){
        menu["component"] = "/layouts/TabLayout"
      }else if(level > 1){
        if(!data.template){
          continue
        }
        menu["component"] = data.template
      }
    }else{
      menu["component"] = !data.template && level == 0 ? "/layouts/TabLayout" : data.template
    }
    menu["icon"] = data.icon
    menu["id"] =  data.id
    menu["text"] =   data.name
    if(data.children){
      menu["children"] = toMenu(data.children, level + 1)
    }
    menus.push(menu)
  }
  return menus;
}